package com.maycari.captainbi.scheduled;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.maycari.captainbi.common.exception.BusinessException;
import com.maycari.captainbi.data.dto.Captain47Dto;
import com.maycari.captainbi.data.dto.Captain49Dto;
import com.maycari.captainbi.data.po.Captain49Po;
import com.maycari.captainbi.service.Captain47Service;
import com.maycari.captainbi.service.Captain49Service;
import com.maycari.captainbi.util.CaptainBIUtil;
import com.maycari.captainbi.util.MaycariRedis;
import com.maycari.tool.utils.BeanUtil;
import com.maycari.tool.utils.DateUtil;
import com.maycari.tool.utils.ThreadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

@Component
public class Task49 implements ApplicationRunner {

    @Autowired
    private MaycariRedis maycariRedis;

    private static String start_time_str = "2021-04-01 00:00:00";

    private static String dataKey = "MAYCARI:DATAKEY49";

    private static String action = "http://cronapi.captainbi.com/index.php?m=cronapi&c=fba&a=getAmazonFbaInventoryV3List";

    @Autowired
    private Captain49Service captain49Service;

    /*@Scheduled(cron = "0 0 1 * * ?")*/
    public void task() {
        System.out.println(1);

    }

    private void getData() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        int page = 1;
        int page_size = 100;

        Date start_time = maycariRedis.get(dataKey);
        if (start_time == null) {
            start_time = DateUtil.parse(start_time_str, DateUtil.PATTERN_DATETIME);
        }

        Date now = new Date();

        JSONArray jsonArray = new JSONArray();

        while (true) {
            //一天
            Date end_time = DateUtil.minusSeconds(DateUtil.plusDays(start_time, 1), 1);

            if (end_time.after(now)) {
                break;
            }

            while (true) {
                ThreadUtil.sleep(1000);
                TreeMap<String, String> params = new TreeMap<>();
                params.put("page", String.valueOf(page));
                params.put("page_size", String.valueOf(page_size));
                params.put("start_time", simpleDateFormat.format(start_time));
                params.put("end_time", simpleDateFormat.format(end_time));
                String result = CaptainBIUtil.sendRequest(action, params);
                JSONObject jsonObject = JSON.parseObject(result, Feature.OrderedField);
                int code = jsonObject.getIntValue("code");
                if (code == 200) {
                    JSONArray data = jsonObject.getJSONArray("data");
                    if (data != null && data.size() > 0) {
                        if (data.size() >= 100) {
                            //可能还有数据  继续请求
                            jsonArray.addAll(data);
                            page++;
                        } else {
                            jsonArray.addAll(data);
                            break;
                        }
                    } else {
                        break;
                    }
                } else {
                    throw new BusinessException(String.format("Task1 error :【%s】", jsonObject.getString("msg")));
                }
            }
            start_time = DateUtil.plusSeconds(end_time, 1);
        }
        JSONArray objects = JSON.parseArray(String.valueOf(jsonArray));
        for (int i = 0; i < objects.size(); i++) {
            JSONObject jsonObject1 = objects.getJSONObject(i);
            String yjzhz = jsonObject1.getString("yjzhz");
            String available_days = jsonObject1.getString("available_days");
            String reserved_customerorders = jsonObject1.getString("reserved_customerorders");
            String inbound_receiving_quantity = jsonObject1.getString("inbound_receiving_quantity");
            String glhz = jsonObject1.getString("glhz");
            String ccf = jsonObject1.getString("ccf");
            String ccf_every = jsonObject1.getString("ccf_every");
            String ltsf_12 = jsonObject1.getString("ltsf_12");
            String month_age12 = jsonObject1.getString("_12_month_age");
            String day_sale = jsonObject1.getString("day_sale");
            String ltsf_6_12 = jsonObject1.getString("ltsf_6_12");
            String total_quantity = jsonObject1.getString("total_quantity");
            String id = jsonObject1.getString("id");
            String ltsf_6_12_uk = jsonObject1.getString("ltsf_6_12_uk");
            String fulfillable_quantity = jsonObject1.getString("fulfillable_quantity");
            String create_time = jsonObject1.getString("create_time");
            String month_age69 = jsonObject1.getString("_6_9_month_age");
            String volume = jsonObject1.getString("volume");
            String unsellable_quantity = jsonObject1.getString("unsellable_quantity");
            String month_age36 = jsonObject1.getString("_3_6_month_age");
            String mfn_listing_exist = jsonObject1.getString("mfn_listing_exist");
            String parent_asin = jsonObject1.getString("parent_asin");
            String seller_sku = jsonObject1.getString("seller_sku");
            String suggested_replenishment_time = jsonObject1.getString("suggested_replenishment_time");
            String month_age3 = jsonObject1.getString("_3_month_age");
            String replenishment_quantity = jsonObject1.getString("replenishment_quantity");
            String doi = jsonObject1.getString("doi");
            String inbound_working_quantity = jsonObject1.getString("inbound_working_quantity");
            String mfn_fulfillable_quantity = jsonObject1.getString("mfn_fulfillable_quantity");
            String ccf_every_uk = jsonObject1.getString("ccf_every_uk");
            String reserved_time = jsonObject1.getString("reserved_time");
            String ltsf_6_12_num = jsonObject1.getString("ltsf_6_12_num");
            String modified_time = jsonObject1.getString("modified_time");
            String ltsf_12_uk = jsonObject1.getString("ltsf_12_uk");
            String glccf_uk = jsonObject1.getString("glccf_uk");
            String health_time = jsonObject1.getString("health_time");
            String reserved_qty = jsonObject1.getString("reserved_qty");
            String glccf = jsonObject1.getString("glccf");
            String month_age912 = jsonObject1.getString("_9_12_month_age");
            String reserved_fc_transfers = jsonObject1.getString("reserved_fc_transfers");
            String yjzhz_zb = jsonObject1.getString("yjzhz_zb");
            String ltsf = jsonObject1.getString("ltsf");
            String reserved_fc_processing = jsonObject1.getString("reserved_fc_processing");
            String ltsf_uk = jsonObject1.getString("ltsf_uk");
            String inbound_shipped_quantity = jsonObject1.getString("inbound_shipped_quantity");
            String amazon_goods_id = jsonObject1.getString("amazon_goods_id");
            String fba_time = jsonObject1.getString("fba_time");
            String asin = jsonObject1.getString("asin");
            String ltsf_num = jsonObject1.getString("ltsf_num");
            String ccf_uk = jsonObject1.getString("ccf_uk");
            String available_stock = jsonObject1.getString("available_stock");
            String ltsf_12_num = jsonObject1.getString("ltsf_12_num");

            Captain49Po captain49Po = new Captain49Po();
            captain49Po.setAmazonGoodsId(jsonObject1.getLong("amazon_goods_id"));
            captain49Po.setAsin(asin);
            captain49Po.setAvailableDays(Integer.valueOf(available_days));
            captain49Po.setCcf(new BigDecimal(ccf));
            captain49Po.setAvailableStock(Integer.valueOf(available_stock));
            captain49Po.setCcfEvery(new BigDecimal(ccf_every));
            captain49Po.setCcfEveryUk(new BigDecimal(ccf_every_uk));
            captain49Po.setCcfUk(new BigDecimal(ccf_uk));
            captain49Po.setCreateTime(Integer.valueOf(create_time));
            captain49Po.setDaySale(new BigDecimal(day_sale));
            captain49Po.setDoi(doi);
            captain49Po.setFbaTime(Integer.valueOf(fba_time));
            captain49Po.setFulfillableQuantity(Integer.valueOf(fulfillable_quantity));
            captain49Po.setGlccf(new BigDecimal(glccf));
            captain49Po.setGlccfUk(new BigDecimal(glccf_uk));
            captain49Po.setGlhz(new BigDecimal(glhz));
            captain49Po.setHealthTime(Integer.valueOf(health_time));
            captain49Po.setId(jsonObject1.getLong("id"));
            captain49Po.setInboundReceivingQuantity(Integer.valueOf(inbound_receiving_quantity));
            captain49Po.setInboundShippedQuantity(Integer.valueOf(inbound_shipped_quantity));
            captain49Po.setInboundWorkingQuantity(Integer.valueOf(inbound_working_quantity));
            captain49Po.setLtsf(new BigDecimal(ltsf));
            captain49Po.setLtsf12(new BigDecimal(ltsf_12));
            captain49Po.setLtsf12Num(Integer.valueOf(ltsf_12_num));
            captain49Po.setLtsf12Uk(new BigDecimal(ltsf_12_uk));
            captain49Po.setLtsf612(jsonObject1.getBigDecimal("ltsf_6_12"));
            captain49Po.setLtsf612Num(jsonObject1.getInteger("ltsf_6_12_num"));
            captain49Po.setLtsf612Uk(jsonObject1.getBigDecimal("ltsf_6_12_uk"));
            captain49Po.setLtsfNum(Integer.valueOf(ltsf_num));
            captain49Po.setLtsfUk(new BigDecimal(ltsf_uk));
            captain49Po.setMfnFulfillableQuantity(Integer.valueOf(mfn_fulfillable_quantity));
            captain49Po.setMfnListingExist(Integer.valueOf(mfn_listing_exist));
            captain49Po.setModifiedTime(Integer.valueOf(modified_time));
            captain49Po.setMonthAge3(Integer.valueOf(month_age3));
            captain49Po.setMonthAge12(Integer.valueOf(month_age12));
            captain49Po.setMonthAge36(Integer.valueOf(month_age36));
            captain49Po.setMonthAge69(Integer.valueOf(month_age69));
            captain49Po.setMonthAge912(Integer.valueOf(month_age912));
            captain49Po.setParentAsin(parent_asin);
            captain49Po.setReplenishmentQuantity(new BigDecimal(replenishment_quantity));
            captain49Po.setReservedCustomerorders(Integer.valueOf(reserved_customerorders));
            captain49Po.setReservedFcProcessing(Integer.valueOf(reserved_fc_processing));
            captain49Po.setReservedFcTransfers(Integer.valueOf(reserved_fc_transfers));
            captain49Po.setReservedQty(Integer.valueOf(reserved_qty));
            captain49Po.setReservedTime(Integer.valueOf(reserved_time));
            captain49Po.setSellerSku(seller_sku);
            captain49Po.setSuggestedReplenishmentTime(Integer.valueOf(suggested_replenishment_time));
            captain49Po.setTotalQuantity(Integer.valueOf(total_quantity));
            captain49Po.setUnsellableQuantity(Integer.valueOf(unsellable_quantity));
            captain49Po.setVolume(new BigDecimal(volume));
            captain49Po.setYjzhz(new BigDecimal(yjzhz));
            captain49Po.setYjzhzZb(new BigDecimal(yjzhz_zb));

            Captain49Dto copy = BeanUtil.copy(captain49Po, Captain49Dto.class);
//            captain49Service.insertBatch(copy);

        }
        maycariRedis.set(dataKey, start_time);




    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        /*getData();*/
    }



}
